﻿using System;
using App.Entity;
using App.IServices;
using App.IRepository;
using System.Collections.Generic;
using System.Linq;
using App.Code;

/*!
* 文件描述：一切从简，只为了更懒！
*/
namespace App.Services
{
	public partial class Sys_RoleAuthorizeServices : BaseServices<Sys_RoleAuthorize> , ISys_RoleAuthorizeServices
	{
		ISys_RoleAuthorizeRepository _ISys_RoleAuthorizeRepository;
        ISys_ModuleRepository _ISys_ModuleRepository;
        ISys_ModuleButtonRepository _ISys_ModuleButtonRepository;
        public Sys_RoleAuthorizeServices(ISys_RoleAuthorizeRepository __ISys_RoleAuthorizeRepository, ISys_ModuleRepository __ISys_ModuleRepository, ISys_ModuleButtonRepository __ISys_ModuleButtonRepository)
		{
			this._ISys_RoleAuthorizeRepository = __ISys_RoleAuthorizeRepository;
            _ISys_ModuleRepository = __ISys_ModuleRepository;
            _ISys_ModuleButtonRepository = __ISys_ModuleButtonRepository;
            base._IBaseRepository = __ISys_RoleAuthorizeRepository;
		}

        public bool ActionValidate(string roleId, string moduleId, string action)
        {
            var authorizeurldata = new List<AuthorizeActionModel>();
            var cachedata = CacheFactory.Cache().GetCache<List<AuthorizeActionModel>>("authorizeurldata_" + roleId);
            if (cachedata == null)
            {
                var moduledata = _ISys_ModuleRepository.QueryAll().OrderBy(t => t.SortCode).ToList();
                var buttondata = _ISys_ModuleButtonRepository.QueryAll().OrderBy(t => t.SortCode).ToList();
                var authorizedata = _ISys_RoleAuthorizeRepository.QueryByWhere(t => t.ObjectId == roleId, "ObjectId").ToList();
                foreach (var item in authorizedata)
                {
                    if (item.ItemType == 1)
                    {
                        Sys_Module moduleEntity = moduledata.Find(t => t.Id == item.ItemId);
                        authorizeurldata.Add(new AuthorizeActionModel { Id = moduleEntity.Id, UrlAddress = moduleEntity.UrlAddress });
                    }
                    else if (item.ItemType == 2)
                    {
                        Sys_ModuleButton moduleButtonEntity = buttondata.Find(t => t.Id == item.ItemId);
                        if (moduleButtonEntity == null)
                        {
                            continue;
                        }
                        authorizeurldata.Add(new AuthorizeActionModel { Id = moduleButtonEntity.ModuleId, UrlAddress = moduleButtonEntity.UrlAddress });
                    }
                }
                CacheFactory.Cache().WriteCache(authorizeurldata, "authorizeurldata_" + roleId, DateTime.Now.AddMinutes(5));
            }
            else
            {
                authorizeurldata = cachedata;
            }
            authorizeurldata = authorizeurldata.FindAll(t => t.Id.Equals(moduleId));
            foreach (var item in authorizeurldata)
            {
                if (!string.IsNullOrEmpty(item.UrlAddress))
                {
                    string[] url = item.UrlAddress.Split('?');
                    if (item.Id == moduleId && url[0] == action)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public List<Sys_RoleAuthorize> GetList(string ObjectId)
        {
            return _ISys_RoleAuthorizeRepository.QueryAll().Where(t => t.ObjectId == ObjectId).ToList();
        }

        public List<Sys_Module> GetMenuList(string roleId)
        {
            var data = new List<Sys_Module>();
            if (OperatorProvider.Provider.GetCurrent().IsSystem)
            {
                data = _ISys_ModuleRepository.QueryAll().OrderBy(t => t.SortCode).ToList();
            }
            else
            {
                var moduledata = _ISys_ModuleRepository.QueryAll().OrderBy(t => t.SortCode).ToList();
                var authorizedata = _ISys_RoleAuthorizeRepository.QueryByWhere(t => t.ObjectId == roleId && t.ItemType == 1, "SortCode").ToList();
                foreach (var item in authorizedata)
                {
                    Sys_Module moduleEntity = _ISys_ModuleRepository.QuerySingle(t => t.Id == item.ItemId);
                    if (moduleEntity != null)
                    {
                        data.Add(moduleEntity);
                    }
                }
            }
            return data.OrderBy(t => t.SortCode).ToList();
        }

        public List<Sys_ModuleButton> GetButtonList(string roleId)
        {
            var data = new List<Sys_ModuleButton>();
            if (OperatorProvider.Provider.GetCurrent().IsSystem)
            {
                data = _ISys_ModuleButtonRepository.QueryAll().OrderBy(t => t.SortCode).ToList();
            }
            else
            {
                var buttondata = _ISys_ModuleButtonRepository.QueryAll().OrderBy(t => t.SortCode).ToList();
                var authorizedata = _ISys_RoleAuthorizeRepository.QueryByWhere(t => t.ObjectId == roleId && t.ItemType == 2, "SortCode").ToList();
                foreach (var item in authorizedata)
                {
                    Sys_ModuleButton moduleButtonEntity = buttondata.Find(t => t.Id == item.ItemId);
                    if (moduleButtonEntity != null)
                    {
                        data.Add(moduleButtonEntity);
                    }
                }
            }
            return data.OrderBy(t => t.SortCode).ToList();
        }
    }   
}
